package com.example.rabbitmqdemo.simple;

import com.example.rabbitmqdemo.util.ConnectionUtils;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

public class Receive {
    private static final String QUEUE_NAME = "test_simple_queue";

    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = ConnectionUtils.getConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);

//        DefaultConsumer consumer = new DefaultConsumer(channel) {
//            @Override
//            public void handleDelivery(String consumerTag, Envelope envelope,
//            AMQP.BasicProperties properties, byte[] body) {
//                String msg = new String(body, StandardCharsets.UTF_8);
//                System.out.println("received: " + msg);
//            }
//        };
//        channel.basicConsume(QUEUE_NAME, true, consumer);


//        GetResponse getResponse = channel.basicGet(QUEUE_NAME, false);
//        System.out.println(new String(getResponse.getBody()));
//        channel.basicAck(1,true);
        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
            System.out.println(" [x] Received '" + message + "'");
        };
        channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> { });
        channel.close();
        connection.close();
    }

}
